define(['jquery','jquery-ui-modules/widget','loadPlayer',"matchMedia"],function($){'use strict';$.widget('mage.listProductVideoLoader',$.mage.productVideoLoader,{_create:function(){switch(this.element.data('type')){case'youtube':this.element.listVideoYoutube();this._player=this.element.data('mageListVideoYoutube');break;case'vimeo':this.element.listVideoVimeo();this._player=this.element.data('mageListVideoVimeo');break;case'custom':this.element.listVideoCustom();this._player=this.element.data('mageListVideoCustom');break;default:throw{name:'Video Error',message:'Unknown video type',toString:function(){return this.name+': '+this.message;}};}},});$.widget('mage.listVideoYoutube',$.mage.videoYoutube,{_create:function(){var self=this;this._initialize();this.element.append('
');this._on(window,{'youtubeapiready':function(){var host='https://www.youtube.com';if(self.useYoutubeNocookie){host='https://www.youtube-nocookie.com';} if(self._player!==undefined){return;} self._autoplay=true;if(self._autoplay){self._params.autoplay=1;} if(!self._rel){self._params.rel=0;} $.extend(self._params,self._params.youtube);delete self._params.vimeo;delete self._params.youtube;self._player=new window.YT.Player(self.element.children(':first')[0],{height:self._height,width:self._width,videoId:self._code,playerVars:self._params,host:host,events:{'onReady':function onPlayerReady(event){event.target.playVideo();self.onVideoReady();var loaderFull=$(self.element).closest('.fotorama__stage').find('.loader.gallery-fulls-loader');if($('body').hasClass('catalog-product-view')){var videoWrapper=self.element.closest('.fotorama-video-container');videoWrapper.removeClass('still-loading');} if(loaderFull.length!==0){$(loaderFull).fadeOut(400);}},'onStateChange':function(data){var videoWrapper;if($('body').hasClass('catalog-product-view')){videoWrapper=self.element.closest('.fotorama-video-container');if(videoWrapper.length===0){videoWrapper=self.element.closest('.product-video-wrapper');}}else{videoWrapper=self.element.closest('.product-video-wrapper');} switch(window.parseInt(data.data,10)){case 1:self._playing=true;break;default:self._playing=false;break;} self._trigger('statechange',{},data);if((data.data===window.YT.PlayerState.ENDED||data.data===window.YT.PlayerState.UNSTARTED)&&self._loop){self._player.playVideo();} if(data.data===window.YT.PlayerState.PLAYING){videoWrapper.removeClass('still-loading');} if(data.data===window.YT.PlayerState.ENDED&&self._loop){self._player.playVideo();} if(data.data===window.YT.PlayerState.UNSTARTED&&self._loop){self._player.playVideo();}}}});}});this._loadApi();},onVideoReady:function(){this.element.addClass('video-ready');this.element.trigger('videoReady');if(this._autoplay){this._player.playVideo();}},play:function(){if(this._player&&this._player.playVideo){this._super();}},pause:function(){if(this._player&&this._player.pauseVideo){this._super();}},stop:function(){if(this._player&&this._player.stopVideo){this._super();}},destroy:function(){this._super();this.element.empty();this.element.removeClass('video-ready');}});$.widget('mage.listVideoVimeo',$.mage.videoVimeo,{_create:function(){var additionalParams='',src;this._initialize();this._autoplay=true;if(this._autoplay){additionalParams+='&autoplay=1';} if(this._loop){additionalParams+='&loop=1';} if(this._params.vimeo){$.each(Object.keys(this._params.vimeo),function(key,param){additionalParams+='&'+param+'='+this._params.vimeo[param];}.bind(this));} src='https://player.vimeo.com/video/'+ this._code+'?api=1&player_id=vimeo'+ this._code+ additionalParams;let iframe=$('').attr('frameborder',0).attr('id','vimeo'+this._code).attr('width',this._width).attr('height',this._height).attr('src',src).attr('referrerPolicy','origin').attr('allow','autoplay');this.element.append(iframe);require(['https://player.vimeo.com/api/player.js'],function(Player){this._player=new Player(iframe);this._player.ready().then(function(){this.onVideoReady();}.bind(this));}.bind(this));},onVideoReady:function(){this.element.addClass('video-ready');this.element.trigger('videoReady');},});$.widget('mage.listVideoCustom',$.mage.videoCustom,{onVideoReady:function(){this.element.addClass('video-ready');this.element.trigger('videoReady');},});$.widget('mage.listingProductVideo',{options:{mobileViewport:768,videoData:'',loadDelay:1000,loadDelayPlusByIndex:100,autoplayOnImageLoad:false,videoActiveClass:'blocked-video',closeVideoSelector:'.product-video-close',videoSettings:{showRelated:0,videoAutoRestart:1,autoplay:1,params:{youtube:{autoplay:1,controls:0,disablekb:1,mute:1,loop:1,modestbranding:1,iv_load_policy:3,showinfo:0,fs:0,rel:0},vimeo:{background:1,byline:0,controls:0,dnt:0,muted:1,pip:0}},}},videoData:null,VU:'video-unplayed',PV:'product-video',VID:'video',VI:'vimeo',_create:function(){this.initializeWidget();let parentGrid=this.element.closest('.product-items');if(parentGrid.hasClass('widget-product-carousel')){parentGrid.on('refresh',function(){this.initializeWidget();}.bind(this));}},initializeWidget:function(){if(this._checkForVideoExist()){this._setVideoEvent();this.autoplayOnImageLoad();}},_createVideoData:function(){var videoData={mediaType:'',isBase:true,id:'',provider:''},dataUrl='';videoData.mediaType=this.VID;if(this.options.videoData.mediaType!=='external-video'){videoData.mediaType=this.options.videoData.mediaType;} if(this.options.videoData.videoUrl){dataUrl=this.options.videoData.videoUrl;dataUrl=this.parseURL(dataUrl);videoData.id=dataUrl?dataUrl.id:this.options.videoData.videoUrl;videoData.provider=dataUrl?dataUrl.type:'custom';videoData.videoUrl=this.options.videoData.videoUrl;videoData.useYoutubeNocookie=dataUrl.useYoutubeNocookie;} return videoData;},_checkForVideoExist:function(){if(!this.options.videoData){return false;} if(this.options.videoData.mobileActive===false&&matchMedia("screen and (max-width:"+this.options.mobileViewport+"px)").matches){return false;} var videoData=this._createVideoData();if(videoData.mediaType===this.VID){this.videoData=videoData;return true;} return false;},_clickHandler:function(event){if(event){event.preventDefault();} if(this.element.hasClass(this.VU)&&this.element.find('iframe').length===0){this._createVideoContainer();this.element.removeClass(this.VU);if(this.videoData.provider===this.VI){this._hideLoader();} this.element.find('.'+this.PV).listProductVideoLoader();}else{this._hideLoader();}},_showLoader:function(){this.element.addClass('block-loading');},_hideLoader:function(){this.element.removeClass('block-loading');},_createVideoContainer:function(){var videoSettings=this.options.videoSettings;this.element.find('.'+this.PV).remove();this.element.append('');let productVideo=this.element.find('.'+this.PV);if(this.options.videoActiveClass){productVideo.addClass(this.options.videoActiveClass);if(this.videoData.provider==='youtube'){productVideo.closest('.product-video-wrapper').addClass('still-loading');}} productVideo.data('params',videoSettings.params);productVideo.data('related',videoSettings.showRelated);productVideo.data('loop',videoSettings.videoAutoRestart);productVideo.data('autoplay',videoSettings.autoplay);productVideo.data('type',this.videoData.provider);productVideo.data('code',this.videoData.id);productVideo.data('youtubenocookie',this.videoData.useYoutubeNocookie);productVideo.data('width','100%');productVideo.data('height','100%');productVideo.data('responsive',false);},_setVideoEvent:function(){this.element.on('click tap','.product-video',$.proxy(this._clickHandler,this));this.element.on('videoReady',function(){this._hideLoader();this._showCloseVideo();}.bind(this));},_closeVideoSetEvents:function(){this.element.on('closeVideo',function(){this._unloadVideoPlayer();}.bind(this));this.element.find(this.options.closeVideoSelector).on('click',function(event){this.element.trigger('closeVideo',event);}.bind(this));},_unloadVideoPlayer:function(){this._hideLoader();this.element.find('.'+this.PV).remove();this.element.addClass(this.VU);this._hideCloseVideo();},_hideCloseVideo:function(){this.element.find(this.options.closeVideoSelector).addClass('no-display');},_showCloseVideo:function(){this.element.find(this.options.closeVideoSelector).removeClass('no-display');},autoplayOnImageLoad:function(){if(this.options.autoplayOnImageLoad){this._showLoader();this.calculateDelayByIndex();if(this.element.find('img[loading=lazy]').prop('complete')){setTimeout(function(){this._clickHandler();}.bind(this),this.options.loadDelay);}else{this.element.find('img[loading=lazy]').on('load',function(){setTimeout(function(){this._clickHandler();this.element.find('img[loading=lazy]').off('load');}.bind(this),this.options.loadDelay);}.bind(this))}}},calculateDelayByIndex:function(){if(this.options.loadDelayPlusByIndex){this.options.loadDelay=(this.options.loadDelay)?this.options.loadDelay:0;if(!this.element.is('[data-video-index]')){let gridContainer=this.element.closest('.product-items');if(gridContainer.length){gridContainer.find('.product-image-wrapper').not('[data-video-index]').each(function(i,elem){$(elem).attr('data-video-index',i);});}} let index=(this.element.attr('data-video-index'))?this.element.data('video-index'):0;this.options.loadDelay+=parseInt(index)*this.options.loadDelayPlusByIndex;}},parseHref:function(href){let a=document.createElement('a');a.href=href;return a;},parseURL:function(href,forceVideo){var id,type,ampersandPosition,vimeoRegex,useYoutubeNocookie=false;function _getYoutubeId(srcid){if(srcid){ampersandPosition=srcid.indexOf('&');if(ampersandPosition===-1){return srcid;} srcid=srcid.substring(0,ampersandPosition);} return srcid;} if(typeof href!=='string'){return href;} href=this.parseHref(href);if(href.host.match(/youtube\.com/)&&href.search){id=href.search.split('v=')[1];if(id){id=_getYoutubeId(id);type='youtube';}}else if(href.host.match(/youtube\.com|youtu\.be|youtube-nocookie.com/)){id=href.pathname.replace('/shorts','').replace(/^\/(embed\/|v\/)?/,'').replace(/\/.*/,'');type='youtube';if(href.host.match(/youtube-nocookie.com/)){useYoutubeNocookie=true;}}else if(href.host.match(/vimeo\.com/)){type='vimeo';vimeoRegex=new RegExp(['https?:\\/\\/(?:www\\.|player\\.)?vimeo.com\\/(?:channels\\/(?:\\w+\\/)','?|groups\\/([^\\/]*)\\/videos\\/|album\\/(\\d+)\\/video\\/|video\\/|)(\\d+)(?:$|\\/|\\?)'].join(''));id=href.href.match(vimeoRegex)[3];} if((!id||!type)&&forceVideo){id=href.href;type='custom';} return id?{id:id,type:type,s:href.search.replace(/^\?/,''),useYoutubeNocookie:useYoutubeNocookie}:false;},destroy:function(){this._super();this._unloadVideoPlayer();}});return $.mage.listingProductVideo;});